﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LAB.Manager
{
    public static class BagManager
    {
        public delegate void handler(t_bag bag);

        public static event handler afterInsert;
        public static event handler afterDelete;
        public static event handler afterEdit;


        static BagManager()
        {
            // bag的删除 导致的从属的tag的删除
            BagManager.afterDelete += (bag) =>
            {
                var tags = TagManager.search(new List<int>() { bag.id }, -1, 0, int.MaxValue);
                foreach (var ti in tags)
                {
                    delete(ti.id, bag.editor);
                }
            };

            // bag的status被设置为0 导致的从属的tag的status被设置为0
            BagManager.afterEdit += (bag) =>
            {
                if (bag.status == 0)
                {
                    var tags = TagManager.search(new List<int> { bag.id }, -1, 0, int.MaxValue);
                    foreach (var ti in tags)
                    {
                        edit(ti.id, 0, bag.editor);
                    }
                }
            };
        }



        public static bool insert(string name, string editor)
        {
            var rst = false;
            using (var db = new TagerEntities())
            {
                if (db.t_bag.Count(c => c.name == name) == 0)
                {
                    // status = 0 表示待审批
                    var bag = new t_bag() { name = name, status = 0, editor = editor, dt = DateTime.Now };
                    db.t_bag.Add(bag);
                    db.SaveChanges();
                    rst = true;
                    // trigger
                    if (afterInsert != null)
                    {
                        afterInsert(bag);
                    }
                }
            }
            return rst;
        }



        public static bool delete(int id, string editor)
        {
            var rst = false;


            using (var db = new TagerEntities())
            {
                var bag = db.t_bag.FirstOrDefault(c => c.id == id);
                if (bag != null && (SysUtil.isSysEditor(editor)))
                {
                    db.t_bag.Remove(bag);
                    db.SaveChanges();
                    rst = true;
                    if (afterDelete != null)
                    { afterDelete(bag); }
                }
            }
            return rst;
        }



        /// <summary>
        /// 修改bag
        /// 只能修改它的状态
        /// 相当于审批
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <param name="editor"></param>
        /// <returns></returns>
        public static bool edit(int id, int status, string editor)
        {
            var rst = false;
            using (var db = new TagerEntities())
            {
                var bag = db.t_bag.FirstOrDefault(c => c.id == id);
                if (bag != null && (SysUtil.isSysEditor(editor) || bag.editor == editor))
                {
                    bag.status = status;
                    bag.dt = DateTime.Now;
                    db.SaveChanges();
                    rst = true;
                    if (afterEdit != null)
                    {
                        afterEdit(bag);
                    }
                }
            }
            return rst;
        }



        /// <summary>
        /// 搜索符合当前关键字的bag集合
        /// </summary>
        /// <param name="keyWord"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<t_bag> search(string keyWord, int status, int pageIndex, int pageSize)
        {
            List<t_bag> rst = null;
            using (var db = new TagerEntities())
            {
                IEnumerable<t_bag> list = db.t_bag;
                if (!string.IsNullOrEmpty(keyWord))
                {
                    list = list.Where(c => c.name.Contains(keyWord));
                }
                if (status != -1)
                {
                    list = list.Where(c => c.status == status);
                }
                list = list.Skip(pageIndex * pageSize).Take(pageSize);
                rst = list.ToList();
            }
            return rst;
        }

    }
}
